/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.inter.controller.api;

import cn.simastudio.talos.common.constants.ApiStatusCode;
import cn.simastudio.talos.common.model.JsonResult;
import cn.simastudio.talos.core.model.base.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * Usage: Demo类
 *
 * @author Charkey
 * @date 2015/5/17 11:01
 */
@RestController
@RequestMapping("/api/demo")
public class DemoApiController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoApiController.class);

    // 获取 users 列表
    @RequestMapping(value = "", method = RequestMethod.GET)
    public JsonResult getUsersList() {
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is Users List");
        return result;
    }

    // 创建一个新的 users
    @RequestMapping(value = "", method = RequestMethod.POST)
    public JsonResult createUser(@RequestParam("name") String name) {
        LOGGER.debug("POST parameter: name={}", name);
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is result POST for create user. Parameter: " + name);
        return result;
    }

    // 获取一个单独的 user
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JsonResult getUser(@PathVariable("id") Integer id) {
        LOGGER.debug("GET parameter: id={}", id);
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is User GET by id. Parameter: " + id);
        return result;
    }

    // 更新 user #id
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public JsonResult updateUser(@PathVariable("id") Integer id, @RequestParam("name") String name) {
        LOGGER.debug("PUT parameter: id={}, name={} ", id, name);
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is User PUT by id. Parameter: id=" + id + " name=" + name);
        return result;
    }

    // 更新 user #id
    @RequestMapping(value = "/users2/{id}", method = RequestMethod.PUT)
    public JsonResult updateUserByUser(@PathVariable("id") Integer id, User User) {
        LOGGER.debug("PUT parameter: id={}, name={}", id, User.getUsername());
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is User PUT by id. Parameter: id=" + id + " username=" + User.getUsername());
        return result;
    }

    // 部分更新 user #id
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public JsonResult patchUser(@PathVariable("id") Integer id, @RequestParam("name") String name) {
        LOGGER.debug("PATCH parameter: id={}, name={}", id, name);
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is User PATCH by id. Parameter: id=" + id + " name=" + name);
        return result;
    }

    // 删除 user #id
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult deleteUser(@PathVariable("id") Integer id) {
        LOGGER.debug("DELETE parameter: id={}", id);
        JsonResult result = new JsonResult();
        result.setCode(ApiStatusCode.OK);
        result.setMessage("Message is optional for successful response");
        result.setData("This is User DELETE by id. Parameters: " + id);
        return result;
    }

}
